/**
 * 
 */
package com.alkandros.calculator.manager;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * @author Jiju Induchoodan
 *
 */
public class Utils 
{
	
	
	public static boolean isAbove16()
	{
		boolean isAbove =false;
		if(Build.VERSION.SDK_INT>=16)
		{
			return true;
		}
		return isAbove;
	}
	
	
	public static void setBackground(Context context,View v,Drawable d)
	{
		if(isAbove16())
		{
			v.setBackground(d);
		}
		else
		{
			v.setBackgroundDrawable(d);
		}
	}
	
	public  static Boolean checkInternetConnection(Context context) {
		ConnectivityManager connec = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
		 NetworkInfo netInfo = connec.getActiveNetworkInfo();
		 if (netInfo != null && netInfo.isConnectedOrConnecting())
		 {
		        return true;
		 }
		else
		{
			return false;
		}
	}
	
	
	public static void loadWebUrls(WebView webview, String url,Context context,final CustomProgressDialog progress)
	{
		WebSettings  webSettings  = webview.getSettings();
		webSettings.setAppCacheMaxSize( 5 * 1024 * 1024);
        webSettings.setAppCachePath(context.getCacheDir().getAbsolutePath());
        webSettings.setAllowFileAccess( true );
        webSettings.setAppCacheEnabled( true );
        webSettings.setJavaScriptEnabled( true );
        progress.show();
        webview.setWebViewClient(new WebViewClient() {
            // load url
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
 
            // when finish loading page
            public void onPageFinished(WebView view, String url) {
                if(progress.isShowing()) {
                	progress.dismiss();
                }
            }
        });
        if(checkInternetConnection(context))
        {
        	  webSettings.setCacheMode ( WebSettings.LOAD_DEFAULT );
        }
        else
        {
        	webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        }
        webview.loadUrl(url);
	}

}
